package com.uyaer.wheel.nape
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.game.map.MapCellType;
	import com.uyaer.wheel.texture.TexturePool;
	
	import nape.phys.Body;
	import nape.phys.BodyType;
	import nape.phys.Material;
	import nape.shape.Polygon;
	
	import starling.display.Image;

	/**
	 *刚体对象池 
	 * @author hc
	 * 
	 */	
	public class BodyPool
	{
		private static var boxList:Vector.<Body> = new Vector.<Body>();
		
		public static function getBox(x:Number,y:Number,w:Number,h:Number,type:int):Body{
			var box:Body = null;
			if(boxList.length>0 ){
				box = boxList.pop();
			}else{
				box = new Body(BodyType.STATIC);
			}
			var material:Material = null;
			switch(type){
				case MapCellType.ROCK:
					material = Material.steel();
			}
			box.shapes.add(new Polygon(Polygon.rect(x,y,w,h),material));
			box.space = Common.space;
			box.cbTypes.clear();
			return box;
		}
		
		public static function recycleBox(b:Body):void{
			var textureType:int = b.userData.data.textureType;
			var texture:Image = b.userData.data.texture;
			if(texture){
				TexturePool.recycle(textureType,texture);
			}
			b.userData.data = null;
			b.cbTypes.clear();
			b.shapes.clear();
			boxList.push(b);
		}
	}
}